import {CLEAR_CART,RECEIVE_ADDRESS,RECEIVE_CATEGORYS,RECEIVE_SHOPS,RESET_USER_INFO,RECEIVE_USER_INFO,RECEIVE_GOODS,RECEIVE_RATINGS,RECEIVE_INFO,INCREMENT_FOOD_COUNT,DECREMENT_FOOD_COUNT} from './mutation-types'
import {resAddress,resCategory,resShops,reqUser,reqLogout,reqShopInfo,reqShopRating,reqShopGoods} from '../api/index'

export default {
   // 根据经纬度获取地址
   async getAddress({commit,state}){
      //   console.log(state);
        let  {latitude,longitude} = state
        console.log(latitude,longitude);
        let params = latitude+','+longitude
        
        let result = await resAddress(params)
      //   console.log(result);
       commit(RECEIVE_ADDRESS,result.data)
    },
   //  获取商品分类
    async getCategorys({commit}){
       let result = await resCategory()
       commit(RECEIVE_CATEGORYS,result.data)
    },
   //  根据地址获取店铺信息
    async getShops({commit,state}){
       let {latitude,longitude} = state
       
       let params = {
          latitude,
          longitude
       }
        let result = await resShops(params)
      //   console.log(result);
        commit(RECEIVE_SHOPS,result.data)
    },
   //  登录获取个人信息
    recordUser({commit},{userInfo}){
       commit(RECEIVE_USER_INFO,userInfo)
    },
   //  存储个人信息
    async getUserInfo({commit}){
       let result = await reqUser()
       if (result.code==0) {
           let userInfo = result.data
           commit(RECEIVE_USER_INFO,userInfo)
       }
    },
   //  退出登录
   async getLogout({commit}){
      let result = await reqLogout()
      if (result.code==0) {
         commit(RESET_USER_INFO)
      }
   },
   // 获取商家商品
   async getShopGoods({commit},callback){
       let result = await reqShopGoods()
       if (result.code==0) {
          let goods = result.data
          console.log(goods);
          commit(RECEIVE_GOODS,goods)
           callback && callback()
       }
   },
   // 获取商家个人信息
   async getShopInfo({commit}){
      let result = await reqShopInfo()
      if (result.code==0) {
          let info = result.data
          console.log(info);
          commit(RECEIVE_INFO,info)
       }
   },
   // 获取店铺评价
   async getShopRatings({commit}){
      let result = await reqShopRating()
      if (result.code==0) {
          let ratings = result.data
          console.log(ratings);
          commit(RECEIVE_RATINGS,ratings)
       }
   },
   // 更新购物车数量的方法
   updateFoodCount({commit},{food,isFlag}){
      if (isFlag) { //表示添加
         commit(INCREMENT_FOOD_COUNT,food)
      }else {
         commit(DECREMENT_FOOD_COUNT,food)
      }
   },
   // 清空购物车
   clearCart({commit}){
      commit(CLEAR_CART)
   }

}
